import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    meta:{
        title:'首页'
    },
    children:[{
        path:'user',
        name:'user',
        meta:{
            title:'用户管理'
        },
        component: () => import('../views/user.vue'),
        children:[{
            path:'userList',
            name:'userList',
            meta:{
                title:'用户列表'
            },
            component: () => import('../views/userList.vue'),
        }]
    },{
        path:'rights',
        name:'rights',
        meta:{
            title:'权限管理'
        },
        component: () => import('../views/rights.vue'), 
        children:[{
            path:'rightsList',
            name:'rightsList',
            meta:{
                title:'权限列表'
            },
            component: () => import('../views/rightsList.vue'),
        },{
            path:'rolesList',
            name:'rolesList',
            meta:{
                title:'角色列表'
            },
            component: () => import('../views/rolesList.vue'),
        }]
    }]
  },
  {
    path: '/about',
    name: 'about',
    component: () => import('../views/AboutView.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue')
  }
]

const router = new VueRouter({
  routes
})
// 全局守卫  守卫的是所有的页面
// 把不需要token就可以进入的页面的路由放在白名单中
let whiteArr = ['/login']
router.beforeEach((to,from,next)=>{
    // 放行的条件  只要本地有token 就放行
    if(!localStorage.getItem('token') && whiteArr.includes(to.path) == false){
        next('/login')
    }
    
     next()
})
export default router
